package com.douwong.student.adapter;

import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.douwong.student.R;
import com.douwong.student.activity.ImageActivity;
import com.douwong.student.model.ClassNoteItem;
import com.douwong.student.rxjava.EndPointInterface;

import java.util.ArrayList;

/**
 * 课堂记录内部grideView Adapter
 */
public class ClassFileRecordInnerAdapter extends BaseAdapter {


	private ArrayList<ClassNoteItem> mClassNoteItems;
	private Context context;

	public ClassFileRecordInnerAdapter(ArrayList<ClassNoteItem> classNoteItems, Context context) {
		mClassNoteItems = classNoteItems;

		this.context=context;
	}
	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return mClassNoteItems.size();
	}

	@Override
	public Object getItem(int position) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return 0;
	}
	@Override
	public View getView(final int position, View convertView, ViewGroup parent) {
		Holder holder=null;
		if(convertView==null){
			holder=new Holder();
			convertView=LayoutInflater.from(context).inflate(R.layout.pic_item, null);
			holder.pic=(ImageView) convertView.findViewById(R.id.pic);
			holder.subject=(TextView) convertView.findViewById(R.id.subject);
			convertView.setTag(holder);
		}else{
			holder=(Holder) convertView.getTag();
		}
		if(mClassNoteItems.size()>0){
			try {
				//holder.subject.setText(list.get(position).getSujectName());
				String url=mClassNoteItems.get(position).getFile_url();
				Glide.with(context).load(EndPointInterface.HEAD_PIC+url).placeholder(R.mipmap.pic_1).into(holder.pic);
				holder.pic.setOnClickListener(new OnClickListener() {
					
					@Override
					public void onClick(View v) {
						Intent intent=new Intent();
						ArrayList<String> imgList=new ArrayList<String>();
						for(int i=0;i<mClassNoteItems.size();i++){
							String img=mClassNoteItems.get(i).getFile_url();
							if(img!=null){
								imgList.add(EndPointInterface.HEAD_PIC+img);
							}
						}
						intent.setClass(context, ImageActivity.class);
						intent.putStringArrayListExtra("imgs", imgList);
						intent.putExtra("position",position);
						context.startActivity(intent);
					}
				});
			} catch (Exception e) {
				e.printStackTrace();
			}
			
		}
		return convertView;
	}
	class Holder {
		ImageView pic;
		TextView subject;
	}
}
